package com.encap;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("SXY");
        person.setAge(30);
        person.setSalary(3000);
        System.out.println(person.toString());
    }
}

class Person{
    public String name;
    private int age;
    private double salary;
    public Person(){

    }
    //将构造器和封装相结合,在构造器里写入set方法
    public Person(String name,int age,double salary){
        setName(name);
        setAge(age);
        setSalary(salary);
    }
    //创建get和set方法，alt+ins
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {//设置合理范围
        if (age>=1&&age<=120) {
            this.age = age;
        }else {
            System.out.println("年龄错误，范围：1-120之间！，系统设置了一个默认年龄。");
            this.age = 18;//设置默认年龄
        }
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //用方法返回属性信息

    @Override
    public String toString() {
        return "当前信息为：" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary;
    }
}